package cn.quuan.RandomInfo.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MainController {


    @GetMapping("/main")
    public String main() {
        RandInfo randInfo = new RandInfo();
        for (int i = 1; i <= 100; i++) {

            // 姓氏随机生成
            String familyName = randInfo.randFamilyName();
            // 名字依托于性别产生
            String randName = randInfo.randName(randInfo.randSex());
            String[] fixed = randName.split("-");
            String name = fixed[0];
            int age = randInfo.randAge();
            String birthday = randInfo.randBirthday(age);

            //将随机生成的 yyyy-MM-dd 格式的生日 转为 yyyyMMdd 格式，便于身份号的随机
            String IdBirthday = birthday.toString().replaceAll("-", "");

            String IdNum = randInfo.getRandomID(IdBirthday);

            String tokenUrl = "http://quuan.cn:8080/employeeInfo/insert?employeeId=" + i + "&employeeName=" + familyName.concat(name) +
                    "&Identity=" + IdNum + "&birthday=" + birthday + "&age=" + age + "&Phone=" + randInfo.getTel() + "&Email=" + randInfo.getEmail(6, 9);
            RestTemplate tokenTemplate = new RestTemplate();
            String tokenResponse = tokenTemplate.getForObject(tokenUrl, String.class);
            assert tokenResponse != null;
            if (tokenResponse.equals("fail")) {
                return "fail";
            }
        }

        return "success";
    }


}
